﻿package org.display.graphic
{
	import flash.display.Sprite;
	import flash.events.*;
	import flash.filesystem.File;
	import flash.text.TextField;
	import flash.utils.*;
	
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.managers.*;
	
	import org.data.*;
	import org.display.util.CreateCommandControl;
	import org.net.*;
	import org.utils.*;
	
	public class ClickSquare extends Sprite
	{
		private var obj:Iv
		private var shapeobj:ShapeBase
		private var path:String
		
		var eventName:String
		
		public function ClickSquare(obj:Iv,shapeobj:ShapeBase)
		{
			
			this.obj=obj
			this.shapeobj=shapeobj
	
			this.buttonMode=true
			this.useHandCursor=true

			this.addEventListener(MouseEvent.MOUSE_UP,mouseEvent)	
				
			this.addEventListener(MouseEvent.ROLL_OVER,mouseEvent)	
				
			this.addEventListener(MouseEvent.ROLL_OUT,mouseEvent)	
				
				
			 eventName= Iv(shapeobj.parent).getViewName()+"_"+shapeobj.getEvent().split(".")[shapeobj.getEvent().split(".").length-1]+".as"
				
			 if(obj.contains(shapeobj)){
				 
				 eventName=shapeobj.getEvent().split(".")[shapeobj.getEvent().split(".").length-1]+".as"
				 
			 }
			 
			 path=obj.path.replace("/xml/","/").replace(".xml","/"+eventName).replace("/display/","/commands/")
			 
		     var file:File = File.desktopDirectory.resolvePath(path);
			 
			 if(file.exists){
			    
				 redraw(0x009900)
			 
			 }else{
			 
				 redraw(0xff0000)
				 
			 }
			 
			 

		}
		
		function redraw(color:uint){
		
			graphics.clear()
			graphics.lineStyle(1,0x000000,1)
			graphics.beginFill(color,1)
			graphics.drawEllipse(0,0,20,20)
			graphics.endFill()
				
		}
		
		function modifyViewProxy(){
		
			//file:///D:/abcTest/org/commands/main/KkView/QqView_abc45Event.as

			var arr:Array=path.split("\/")
			
			var pop:String=arr.pop()
			
			//如果是自身事件则不需要修改viewProxy	
			if(pop.indexOf("_")==-1){return;}
				
			pop=arr.pop()
				
			var viewProxyPath:String = arr.join("/").replace(/\/commands\//,"/display/")+"/viewProxy/"+pop+"Proxy.as"
		
			trace(viewProxyPath)
				
			var disEventStr:String=path.replace(/.*?(?=\/org\/)/,"").replace(/\//,"").replace(".as","").replace(/\//gis,".")
		
			var evts:Array=disEventStr.split(".")
				
			evts.splice(evts.length-2,1)
				
			var pop=evts.pop()
				
			evts.push(pop.split("_")[0])
				
			evts.push(pop.split("_")[1])
				
			var addEventStr:String=	evts.join(".")
				
			//trace(addEventStr,disEventStr,viewProxyPath)	
				
			var xmlRes:AbcResponder=new AbcResponder()
			
			xmlRes.onResult=function(e,path){
				
				var source:String=e
					
				var funName:String=disEventStr.split(".")[disEventStr.split(".").length-1]
					
				var repstr:String="//onEnable"
				var newstr:String=repstr+"\n         AbcEventDispatcher.instance.addEventListener(\""+addEventStr+"\","+funName+");"
					
				source=source.replace(repstr,newstr)
					
					
				repstr="//offEnable"
				newstr=repstr+"\n         AbcEventDispatcher.instance.removeEventListener(\""+addEventStr+"\","+funName+");"
						
				source=source.replace(repstr,newstr)
					
					
				repstr="//function"
				newstr=repstr+"\n         public function "+funName+"(e:AbcEvent){    AbcEventDispatcher.instance.dispatcher(\""+disEventStr+"\",view,e);}"
				
				source=source.replace(repstr,newstr)
					
					trace(path+"/////////////")
					
				setTimeout(FileControl.saveFile,100,path,source)	
				
			}
			
			new AbcHTTPServices(viewProxyPath,xmlRes)

		}
		
		
		function createCommand(){
			
			    //file:///D:/abcTest/org/commands/main/KkView/QqView_abc45Event.as
			
			   var arr:Array=path.replace(/.*?(?=\/org\/)/,"").replace(/\//,"").replace(".as","").split("\/")
			
			   var arrcopy:Array=arr.concat()
			   var pop=arrcopy.pop()
			   var package_command=arrcopy.join(".")
			   
			   arrcopy.splice(arrcopy.length-1,0,"view")   
			   var import_commands="import "+arrcopy.join(".").replace(".commands.",".display.")+";\n             "
			
			   
			   var pop1=arrcopy.pop()
			   //如果不是自己就加载
			   if(pop.indexOf("_")!=-1){
			      arrcopy.push(pop.split("_")[0])    
			      import_commands+="import "+arrcopy.join(".").replace(".commands.",".display.")+";\n             "
			   }
			   
			   var type_commands=pop1
			   
			   var name_commands=pop
			   
			   var eventType_commands=pop.split("_")[0]
			
				var commandSource:String=Global.commandSource
				
				commandSource=commandSource.replace(/<package>/gis,package_command)
				commandSource=commandSource.replace(/<import>/gis,import_commands)
				commandSource=commandSource.replace(/<type>/gis,type_commands)
				
				commandSource=commandSource.replace(/<class>/gis,name_commands)
				
					
				if(pop.indexOf("_")==-1){
				
					commandSource=commandSource.replace("//eventView=e.source.view as <eventType>","")
					commandSource=commandSource.replace("//private var eventView:<eventType>","")
				
				}else{
				
					commandSource=commandSource.replace(/<eventType>/gis,eventType_commands)
				
				}	
				
				commandSource=commandSource.replace(/e\.source\.source\.type/gis,"e.source.type")
				
				if(File.desktopDirectory.resolvePath(path).exists==false)FileControl.saveFile(path,commandSource)	
				
			
		}
		
		function alertClickHandler(event:CloseEvent):void {
			
			if (event.detail==Alert.NO){return;}
			
			createCommand()
			
			new CreateCommandControl()
			
			modifyViewProxy()
			
			redraw(0x009900)
			
		} 
		
		
		function mouseEvent(e:MouseEvent){
			
			switch(e.type){
				
			case MouseEvent.MOUSE_UP:
				
			trace(path)
			//打开main.as3proj
			try{
				
			var file:File = File.desktopDirectory.resolvePath(path);
			file.openWithDefaultApplication();
			
			}catch(e:Error){
				
				Alert.yesLabel = "Yes";
				Alert.noLabel = "No";
				Alert.show("事件不存在，是否确认生成？", path.split("/")[path.split("/").length-1], 3, this, alertClickHandler);

			}
			
			break
			
			case MouseEvent.ROLL_OVER:

			var tt:TextField=new TextField()
			tt.text=eventName
			tt.border=true
			tt.autoSize="left"
			tt.background=true
			
			stage.addChild(tt)
			
			tt.x=stage.mouseX+20
			tt.y=stage.mouseY+20
			
			
			break
			
			
			case MouseEvent.ROLL_OUT:
				
				
			ElementControl.cleanObjByType(TextField,stage)
				
				
				
			break

			
			}
			

		}
		
		
		
	}
}